﻿package main 
{
	import common.KeyCode;
	import common.MainSprite;
	import common.Scene;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.media.Sound;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import flash.utils.*;
	
	/**
	 * メインのゲーム画面
	 */
	public class MainScene extends Scene {
		
		private static const GOAL_RADIUS_SQ:Number = 1800;
		
		[Embed(source = '../../lib/Flag01.png')]
		private static const FlagImage:Class;
		[Embed(source = '../../lib/Dolphin.mp3')]
		private static const DolphinSound:Class;

		public function MainScene(main:MainSprite) {
			super(main);
			_sprite = new Sprite();
			
			moviePlayer = new MoviePlayer();
			
			// SeaFieldCircle or SeaFieldQuarter
			seaField = new main.SeaFieldQuarter(200);
			seaField.getSprite().x = 320;
			seaField.getSprite().y = 240;
			
			goalMarker = new Sprite();
			var flagBitmap:Bitmap = new FlagImage();
			flagBitmap.scaleX = flagBitmap.scaleY = 0.25;
			flagBitmap.x = -flagBitmap.width / 2;
			flagBitmap.y = -flagBitmap.height / 2;
			goalMarker.addChild(flagBitmap);
			
			wind = new Wind();
			wind.setAngle(0);
			wind.getSprite().x = 50;
			wind.getSprite().y = 50;
			
			yacht = new Yacht();
			yacht.sprite.x = 320;
			yacht.sprite.y = 300;
			
			_sprite.addChild(seaField.getSprite());
			_sprite.addChild(goalMarker);
			_sprite.addChild(yacht.sprite);
			_sprite.addChild(wind.getSprite());
			_sprite.addChild(moviePlayer.getSprite());
			
			setVisibleArrow(true);
		}
		
		/**
		 * シーンが切り替わったタイミングで呼び出されます。
		 * @param	origin
		 * @param	destination
		 */
		public function init(origin:uint, destination:uint):void {
			flags = FlagMap.getFlags(origin, destination);
			
			if (false) {
				initGamePlaying();
			} else {
				moviePlayer.getSprite().visible = true;
				moviePlayer.play();
				function checkFinished():void {
					var state:int = moviePlayer.getPlayerState();
					if (state == 0) {
						initGamePlaying();
					}
				};
				movieCheckTimer = setInterval(checkFinished, 1000);
			}
		}
		
		private function initGamePlaying():void {
			moviePlayer.getSprite().visible = false;
			curFlag = 0;
			yacht.init();
			_sprite.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			moviePlayer.stop();
			clearInterval(movieCheckTimer);
			movieCheckTimer = 0;
		}
		
		override public function onKeyDown(keyCode:KeyCode):void {
			yacht.onKeyDown(keyCode);
			if (keyCode == KeyCode.OK) {
				if (movieCheckTimer != 0) {
					moviePlayer.stop();
					initGamePlaying();
				} else {
					invokeMapScene(false, false);
				}
			}
		}
		
		private function isYachtTouchingCurrentFlag(dx:Number, dy:Number):Boolean {
			return dx * dx + dy * dy < GOAL_RADIUS_SQ;
		}
		
		private function onEnterFrame(event:Event):void {
			//wind.setAngle(wind.getAngle() + 0.2);
			yacht.giveWind(wind);
			seaField.advance(1);
			// 今の実装では不自然なので海面は回さない
			//seaField.sprite.rotation = -yacht.getHelmAngle();
			wind.getSprite().rotation = wind.getAngle() + yacht.getAngle();
			
			var flagPos:Flag = flags[curFlag];
			var dx:Number = flagPos.getX() - yacht.getX();
			var dy:Number = flagPos.getY() - yacht.getY();
			
			if (isYachtTouchingCurrentFlag(dx, dy)) {
				dolphinSound.play();
				if (curFlag + 1 < flags.length) {
					curFlag++;
				} else {
					// ゴール
					_sprite.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
					closeTimer = setInterval(function():void {
						sprite.alpha -= 0.1;
						if (sprite.alpha <= 0) {
							clearInterval(closeTimer);
							closeTimer = 0;
							sprite.alpha = 1;
							invokeMapScene(false, true);
						}
					}, 100);
					return;
				}
			}
			
			flagPos.updateSpriteQuarter(goalMarker, yacht, dx, dy);
			seaField.limitFlagMarkerPosition(goalMarker);
		}
		
		public function get sprite():Sprite {
			return _sprite;
		}
		
		private var seaField:SeaField;
		private var wind:Wind;
		private var yacht:Yacht;
		
		private var flags:Array;
		private var curFlag:int;
		
		private var goalMarker:Sprite;
		
		private var moviePlayer:MoviePlayer;
		private var movieCheckTimer:uint;
		private var closeTimer:uint;
		private var dolphinSound:Sound = new DolphinSound();
		
	}
	
}